﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;

namespace ProcessesSMS
{
    /// <summary>
    /// Summary description for.
    /// game name: THI TIM HIEU HA NOI VA THI TRUONG CHUNG KHOAN 
    /// channel: n/a
    /// command code: HN X Y
    /// created: 20/03/07 .
    /// Writer : Hungncv .
    /// </summary>
    /// 
    //--Tren bao ANTD-->///
    ///AN_X_Y : 8359 (X: A,B,C,D : Chuong trinh HN ngan nam van hien)
    public class DS
    {
        #region Declare
        private SMS_MO WMO;
        private SMS_MT WMT;
        ArrayList arr = new ArrayList();

        public DS(SMS_MO MO)
        {
            WMO = MO;
        }

        public DS()
        {
        }
        #endregion
        #region Method
        public void run()
        {
            WMT = new SMS_MT(WMO);
            //SMS_STR WSTR = new SMS_STR();

            string tmpMsg = WMO.Message.Substring(WMO.CommandCode.Length).Trim().Replace("  ", " ").Replace("  ", " ");
            tmpMsg = tmpMsg.Replace("  ", " ").Trim().ToUpper();           
            string[] msg = tmpMsg.Split(new char[] { ' ' });

            string strReturnMsg = "";
            strReturnMsg = GetReturnMsg(msg);
			if(strReturnMsg.Length>160)
			{
				string supertemp="";
				supertemp = strReturnMsg;
				strReturnMsg = strReturnMsg.Substring(0,160);
				WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
				WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;

				strReturnMsg = supertemp.Substring(160);
				WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
				WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
				//
				WMT.Insert();
			}
			else
			{
				WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
				WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
				WMT.Insert(); 
			}
            return;

        }
        private bool ValidInput(string[] tmp)
        {
            if (tmp.Length <= 0 || tmp.Length > 2) return false;
            return true;
        }
        private bool checkIsNumber(string txNumber)
        {
            bool textIsNumeric = true;
            try
            {
                int.Parse(txNumber.Trim());
            }
            catch
            {
                textIsNumeric = false;
            }
            return textIsNumeric;
        }
		private int CheckExist(string str, string Item)
		{	
			int totalView = 0;
			string[] m_str = str.Split('-');
			int totalItem = m_str.Length;
			string tmp = "";
			int lenItem = 0;
			if(str.Trim()!="")
			{
				for (int j = 0; j < totalItem; j++)
				{
					lenItem = m_str[j].Length;
					tmp = m_str[j].Substring(lenItem - 2, 2);
					if (int.Parse(tmp) == int.Parse(Item))
					{
						totalView++;
					}
				}
			}
			return totalView;
		}
		//Lay toan bo ket qua tra ve trong 30 ngay
		private string GetResult(DataSet ds,string key)
		{				
			string strFin="";          
			int strResult=0;
			string strgiai = "";
			int total = 0;          
			//kiem tra 30 ngày trờ về trước
			for (int k = 0; k < ds.Tables[0].Rows.Count; k++)
			{
				//lap tu giai 1 den giai 8 trong 1 ngay
				//ket qua cua mot ngay				
				strgiai="";				
				total=0;
				#region Kiem tra cac giai
				//giai bac biet
				strResult = CheckExist(ds.Tables[0].Rows[k]["giaidacbiet"].ToString().Trim(), key);
				if (strResult>0)
				{
					strgiai += "DB ";
					total += strResult;
				}
				//giai 1
				strResult = CheckExist( ds.Tables[0].Rows[k]["giainhat"].ToString().Trim(), key);
				if (strResult>0)
				{
					strgiai += "1 ";
					total += strResult;
				}
				//giai 2
				strResult = CheckExist(ds.Tables[0].Rows[k]["giainhi"].ToString().Trim(), key);
				if (strResult>0)
				{
					strgiai += "2 ";
					total += strResult;
				}
				//giai 3
				strResult = CheckExist(ds.Tables[0].Rows[k]["giaiba"].ToString().Trim(), key);
				if (strResult>0)
				{
					strgiai += "3 ";
					total +=strResult;
				}
				//giai 4
				strResult = CheckExist(ds.Tables[0].Rows[k]["giaitu"].ToString().Trim(), key);
				if (strResult>0)
				{
					strgiai += "4 ";
					total += strResult;
				}
				//giai 5
				strResult = CheckExist(ds.Tables[0].Rows[k]["giainam"].ToString().Trim(), key);
				if (strResult>0)
				{
					strgiai += "5 ";
					total += strResult;
				}
				//giai 6
				strResult = CheckExist(ds.Tables[0].Rows[k]["giaisau"].ToString().Trim(), key);
				if (strResult>0)
				{
					strgiai += "6 ";
					total += strResult;
				}
				//giai 7
				strResult = CheckExist(ds.Tables[0].Rows[k]["giaibay"].ToString().Trim(), key);
				if (strResult>0)
				{
					strgiai += "7 ";
					total += strResult;
				}
				//giai 8
				strResult = CheckExist(ds.Tables[0].Rows[k]["giaitam"].ToString().Trim(), key);
				if (strResult>0)
				{
					strgiai += "8 ";
					total += strResult;
				}
				#endregion
				DateTime d =DateTime.Parse(ds.Tables[0].Rows[k]["ngay"].ToString());				
				if(total>0)
				{					
					strFin +="Ngay:" + d.ToString("dd/MM/yy") + "\n"; 					
					strFin+="So luong:" + total + "\n";
					strFin+="Giai:" + strgiai.Trim().Replace(" ",",") + "\n";
				}
			}
			return strFin;
		}
        private string GetReturnMsg(string[] arr)
        {
            string connString = ConfigurationSettings.AppSettings["CONNECTION_STRING_7x62"].ToString();
            SqlConnection oledbConn = new SqlConnection(connString);
            string strSQl = string.Empty;
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter oleda = new SqlDataAdapter();
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            string code = string.Empty;
            string msgBody = string.Empty;           
            oledbConn.Open();
            /**/
            if (WMO.ServiceID.Replace("04","") == "6072")
            {
				msgBody = ConfigSettings.ReadSetting("SMS_HELP");		
            }
            else if (WMO.ServiceID.Replace("04","") == "6172")
            {
				msgBody = ConfigSettings.ReadSetting("SMS_HELP");
            }
            else if (WMO.ServiceID.Replace("04","") == "6272")
            {
				if(arr[0].ToUpper()=="TD")
				{
					strSQl = "select * from xoso where Mamien='MB' and (getdate()-ngay)<=30";}
				else
				{
					strSQl = "select * from xoso where Matinh='" + arr[0].ToUpper() + "' and (getdate()-ngay)<=30";}
				cmd = new SqlCommand(strSQl, oledbConn);
				oleda = new SqlDataAdapter();
				oleda.SelectCommand = cmd;
				ds = new DataSet();
				oleda.Fill(ds, "VIT");
				dt = ds.Tables["VIT"];
				int totalrow=ds.Tables[0].Rows.Count;
				if (totalrow > 0)
				{
					msgBody=GetResult(ds,arr[1]);
					if(msgBody.Trim()=="")
						msgBody=arr[1] + " chua ve ngay nao trong 30 ngay gan day";
				}		
            }
            else if (WMO.ServiceID.Replace("04","") == "6372")
            {
                msgBody = ConfigSettings.ReadSetting("SMS_HELP");
            }
            else if (WMO.ServiceID.Replace("04","") == "6572")           																			
            {
                msgBody = ConfigSettings.ReadSetting("SMS_HELP");
            }
            else if (WMO.ServiceID.Replace("04","") == "6772")           
            {
               msgBody = ConfigSettings.ReadSetting("SMS_HELP");
            }
            else
            {
                msgBody = ConfigSettings.ReadSetting("SMS_HELP");
            }

            oledbConn.Close();
            return msgBody;
        }

        #endregion Method
    }
}
